package com.hc.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    /**
     * 请求响应状态码（200、400、500）
     */
    private int code;
    /**
     * 请求结果描述信息
     */
    private String msg;
    /**
     * 请求结果数据
     */
    private T data;

    private Result(ResultCode resultCode) {
        if (resultCode == null) {
            return;
        }
        this.code = resultCode.code();
        this.msg = resultCode.msg();
    }

    private Result(ResultCode resultCode, T data) {
        this(resultCode);
        this.data = data;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static <T> Result<T> success() {
        Result result = new Result(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 操作成功
     *
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result result = success();
        result.setData(data);
        return result;
    }

    /**
     * 操作成功
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static <T> Result<T> success(ResultCode resultCode, T data) {
        Result result = new Result(resultCode, data);
        return result;
    }

    /**
     * 操作失败
     *
     * @param resultCode
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode) {
        Result result = new Result(resultCode);
        return result;
    }

    /**
     * 操作失败
     *
     * @param resultCode
     * @param data
     * @return
     */
    public static <T> Result<T> fail(ResultCode resultCode, T data) {
        Result result = new Result(resultCode, data);
        return result;
    }
}
